package com.grocery.controller;

import com.grocery.bean.Cart;
import com.grocery.bean.Cartitem;
import com.grocery.bean.User;
import com.grocery.dao.CartDao;
import com.grocery.service.impl.CartService;
import com.grocery.util.SYS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("cart")
public class CartController {

    @Autowired
    CartService cartService;

    @RequestMapping("add")
    @ResponseBody
    public int add(int productid,int userid) {
        return cartService.add(productid,userid);
    }

    @RequestMapping("addcart")
    @ResponseBody
    public int add(int productid, HttpServletRequest request) {
        HttpSession session=request.getSession();
        User u= (User) session.getAttribute(SYS.USER_LOGIN);
        if(u==null) {
            return 500;
        }else{
            int userid=u.getId();
            System.out.println(userid);
            return cartService.add(productid,userid);
        }
    }


    @RequestMapping("addnum")
    @ResponseBody
    public int addnum(int productid,int userid,int number) {
        return cartService.addnum(productid,userid,number);
    }

    @RequestMapping("delete")
    @ResponseBody
    public int delete(int productid,int userid) {
        return cartService.delete(productid,userid);
    }

    @RequestMapping("drop")
    @ResponseBody
    public int drop(int productid,int userid) {
        return cartService.drop(productid,userid);
    }

    @RequestMapping("update")
    @ResponseBody
    public int update(int productid,int userid,int number) {
        return cartService.update(productid,userid,number);
    }

    @RequestMapping("getcartData")
    @ResponseBody
    public List<Cart> getCartData(int userid) {
        return cartService.getcartData(userid);
    }

}
